
package com.xinzhidi.web.city.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xinzhidi.common.utils.DateUtils;
import com.xinzhidi.common.utils.WeatherUtil;
import com.xinzhidi.web.city.domain.City;
import com.xinzhidi.web.city.domain.CityWeather;
import com.xinzhidi.web.city.mapper.CityWeatherMapper;
import com.xinzhidi.web.plot.service.impl.CPlotServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName City
 * @Description:
 * @Author cx
 * @Date 2023-05-23 00:03
 * @Version V1.0
 **/
@Service
@RequiredArgsConstructor
public class CityWeatherService extends ServiceImpl<CityWeatherMapper, CityWeather> {

    private final CPlotServiceImpl plotService;

    public void weatherSaveTask() {
        // 1.获取所有小区的id和城市id
        // 2.获取城市id之后查询24小时天气
        // 3.将获取到的天气保存
        List<City> cityList = plotService.getPlotCityWeather();
        if (CollectionUtils.isEmpty(cityList)) {
            return;
        }

        // 定义24小时天气token和path
        String token = "008d2ad9197090c5dddc76f583616606";
        String path = "/whapi/json/alicityweather/forecast24hours";
        ObjectMapper objectMapper = new ObjectMapper();

        List<CityWeather> cityWeathers = Lists.newArrayList();
        CityWeather cityWeather;
        for (City city : cityList) {
            String weather = WeatherUtil.getWeather(String.valueOf(city.getCityId()), token, path);
            try {

                Map<String, Object> map = objectMapper.readValue(weather, Map.class);
                Map<String, Object> data = (Map<String, Object>) map.get("data");
                List<Map<String, Object>> hourlyList = (List<Map<String, Object>>) data.get("hourly");
                if (CollectionUtils.isEmpty(hourlyList)) {
                    continue;
                }



                for (Map<String, Object> m : hourlyList) {
                    //如果时间小于当前时间，则不保存
                    Date date = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, m.get("date") + " " + m.get("hour") + ":00:00");
                    if (date.before(new Date())) {
                        continue;
                    }
                    cityWeather = this.getOne(new LambdaQueryWrapper<>(CityWeather.class)
                            .eq(CityWeather::getPlotId, city.getCityId())
                            .eq(CityWeather::getTimePoint , date));
                    if(cityWeather != null){
                        continue;
                    }
                    cityWeather = new CityWeather();
                    cityWeather.setCityId(city.getCityId());
                    cityWeather.setPlotId(Integer.parseInt(city.getId()));
                    cityWeather.setTemperature(new BigDecimal(m.get("temp").toString()));
                    cityWeather.setTimePoint(date);
                    cityWeathers.add(cityWeather);
                }
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
            if (CollectionUtils.isEmpty(cityWeathers)) {
                return;
            }
        }
//        this.saveBatch(cityWeathers);
        this.batchInsert(cityWeathers);
    }

    /**
     * 批量插入
     *
     * @param cityWeathers
     */
    private void batchInsert(List<CityWeather> cityWeathers) {
        this.baseMapper.batchInsert(cityWeathers);
    }

}
